import 'package:flutter/material.dart';
import 'package:interactive_webview/interactive_webview.dart';
import 'package:flutter/services.dart';
import 'jsFramework.dart';

InteractiveWebView CMiniapp;

class MiniApp {
  MiniApp() {
    CMiniapp = new InteractiveWebView();
    CMiniapp.didReceiveMessage.listen(_onReceivedMessage);

    List<String> _js = [
      "assets/jsFramework/tools.js",
      "assets/jsFramework/jsChannel.js",
      "assets/jsFramework/api.js"
    ];

    _js.forEach((item) async {
      String scrpit = await rootBundle.loadString(
        item,
        cache: false,
      );
      CMiniapp.evalJavascript(scrpit);
    });
  }

  _onReceivedMessage(WebkitMessage message) async {
    ScriptDataModel sm = ScriptDataModel.fromJson(message.data);
    switch (sm.action) {
      case "print":
        print(sm.data);
        break;
      default:
       

        // print();
        break;
    }
  }

  // _sendSuccess(String uuid, String data) {
  //   CMiniapp.evalJavascript("jsChannel.send['$uuid'].resolve('$data')");
  // }

  // _sendFail(String uuid, String error) {
  //   CMiniapp.evalJavascript("jsChannel.send['$uuid'].reject('$error')");
  // }
}
